package cn.com.jinhao.provider.business.controller;

import cn.com.jinhao.hotline.commons.controller.BaseController;
import cn.com.jinhao.hotline.commons.exception.ParamErrorException;
import cn.com.jinhao.hotline.commons.util.RestResponse;
import cn.com.jinhao.hotline.commons.vo.system.SysDictRequestVO;
import cn.com.jinhao.provider.business.entity.Addr;
import cn.com.jinhao.provider.business.entity.SysDict;
import cn.com.jinhao.provider.business.service.AddrService;
import cn.com.jinhao.provider.business.service.SysDictService;
import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.*;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 系统字典表(TbSysDict)表控制层
 *
 * @author makejava
 * @since 2020-05-15 14:20:17
 */
@RestController
@RequestMapping("/system")
@Api(tags = {"系统字典服务"})
public class SysDictController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private SysDictService sysDictService;

    @Resource
    private AddrService addrService;

    /**
     * 查询系统字典信息
     * @param paid   父编码   (如果为SmallType，表示查询所有事件小类）
     * @param deptCode 部门编码
     * @param isAll 是否全部
     * @param pageNo 页数
     * @param pageSize   每页记录条数
     * @return  RestResponse
     */
    @ApiOperation(value = "系统字典查询")
    @ApiImplicitParams( {
        @ApiImplicitParam(name = "paid", value = "字典上级编码", required = true),
        @ApiImplicitParam(name = "deptCode", value= "部门编码"),
        @ApiImplicitParam(name = "isAll", value = "是否全部",dataType = "Boolean", required = true, defaultValue = "false", allowableValues = "true,false"),
        @ApiImplicitParam(name = "pageNo", value = "页数", defaultValue = "1"),
        @ApiImplicitParam(name = "pageSize", value = "记录条数", defaultValue = "20")
    })
    @GetMapping(value = "/sysDict")
    public RestResponse<java.util.Map<String, Object>> querySysDict(@RequestParam String paid, @RequestParam(required = false) String deptCode, @RequestParam boolean isAll, @RequestParam int pageNo, @RequestParam int pageSize)  throws Exception {
        if (!isAll) {
            if (pageNo <= 0) {
                throw new ParamErrorException("pageNo必须大于0");
            }
            if (pageSize <= 0) {
                throw new ParamErrorException("pageSize必须大于0");
            }
            return new RestResponse<>(sysDictService.querySysDict(paid,deptCode,pageNo,pageSize));
        }else {
            return new RestResponse<>(sysDictService.querySysDict(paid,deptCode,0,0));
        }

    }

    @ApiOperation(value = "通过编码获得字典信息")
    @GetMapping(value = "/sysDict/getEventType")
    public RestResponse<SysDict> queryEventTypeByCode(@ApiParam(name = "code", value = "字典编码", required = true) @RequestParam String code, @RequestParam String paid) {
        return new RestResponse<>(sysDictService.queryByCodeAndPaid(code, paid));
    }

    @ApiOperation(value = "新增系统字典")
    @PostMapping(value = "/sysDict")
    public RestResponse<SysDict> addSysDict(@ApiParam(name = "sysDictRequestVO", value = "系统字典信息", required = true) @Validated(SysDictRequestVO.Create.class) @RequestBody SysDictRequestVO sysDictRequestVO, BindingResult result) throws Exception {
        validate(result);
        SysDict sysDict = new SysDict();
        BeanUtil.copyProperties(sysDictRequestVO, sysDict);
        sysDictService.add(sysDict);
        return new RestResponse<>(sysDict);
    }

    @ApiOperation(value = "更新系统字典")
    @PutMapping(value = "/sysDict")
    public RestResponse<SysDict> updateSysDict(@ApiParam(name = "sysDictRequestVO", value = "系统字典信息", required = true) @Validated(SysDictRequestVO.Update.class) @RequestBody SysDictRequestVO sysDictRequestVO, BindingResult result) throws Exception {
        validate(result);
        SysDict sysDict = new SysDict();
        BeanUtil.copyProperties(sysDictRequestVO, sysDict);
        if(sysDict.getId().isEmpty()){
            sysDictService.update(sysDict);
        }else{
            sysDictService.updateById(sysDict);
        }
        return new RestResponse<>(sysDict);
    }

    /**@ApiOperation(value = "删除系统字典")
    @ApiImplicitParams( {
        @ApiImplicitParam(name = "code", value = "编码", required = true),
        @ApiImplicitParam(name = "paid", value = "父编码")
    })
    @DeleteMapping(value = "/sysDict")
    public RestResponse<String> deleteSysDict(@RequestParam String code, @RequestParam String paid) throws Exception {
        sysDictService.delete(code, paid);
        return new RestResponse<>(null);
    }*/

    @ApiOperation(value = "删除系统字典")
    @ApiImplicitParams( {
        @ApiImplicitParam(name = "id", value = "id", required = true),
    })
    @DeleteMapping(value = "/sysDict")
    public RestResponse<String> deleteSysDictByid(@RequestParam String id) throws Exception {
        sysDictService.delete(id);
        return new RestResponse<>(null);
    }

    @ApiOperation(value = "通过地区父编码获得地区信息")
    @ApiImplicitParams( {
        @ApiImplicitParam(name = "paid", value = "上级编码", required = true),
        @ApiImplicitParam(name = "isAll", value = "是否全部",dataType = "Boolean", required = true, defaultValue = "false", allowableValues = "true,false"),
        @ApiImplicitParam(name = "pageNo", value = "页数", defaultValue = "1"),
        @ApiImplicitParam(name = "pageSize", value = "记录条数", defaultValue = "20")
    })
    @GetMapping( value = "/area")
    public RestResponse<java.util.Map<String, Object>> queryArea(@RequestParam String paid, @RequestParam boolean isAll, @RequestParam int pageNo, @RequestParam int pageSize) throws Exception {
        if (!isAll) {
            if (pageNo <= 0) {
                throw new ParamErrorException("pageNo必须大于0");
            }
            if (pageSize <= 0) {
                throw new ParamErrorException("pageSize必须大于0");
            }
            return new RestResponse<>(addrService.queryByPaid(paid,pageNo,pageSize));
        }else {
            return new RestResponse<>(addrService.queryByPaid(paid,0,0));
        }

    }

    @ApiOperation(value = "通过地区编码获得地区信息")
    @GetMapping( value = "/area/queryByCode")
    public RestResponse<Addr> queryAreaByCode(@ApiParam(name = "code", value = "地区编码", required = true)@RequestParam String code) {
        return new RestResponse<>(addrService.queryByCode(code));
    }

}